package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.TitanAgpsCallback;
import com.apex.bluetooth.callback.TitanSerialNumberCallback;
import com.apex.bluetooth.callback.TitanSwitchCallback;
import com.apex.bluetooth.callback.TitanUnitFormatCallback;
import com.apex.bluetooth.callback.TitanVersionCallback;
import com.apex.bluetooth.callback.TitanVibrateCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.TiTanFormat;
import com.apex.bluetooth.model.TitanAgpsInfo;
import com.apex.bluetooth.model.TitanSwitchInfo;
import com.apex.bluetooth.model.TitanVersion;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityTitanBinding;
import com.apex.sdk.dialog.DeviceUnitDialog;
import com.apex.sdk.dialog.HourSystemDialog;
import com.apex.sdk.dialog.MonthDialog;
import com.apex.sdk.dialog.SwitchDialog;
import com.apex.sdk.dialog.TemperatureUnitDialog;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.IllegalFormatCodePointException;


public class TitanActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    SwitchDialog notifyDialog, brightDialog, disturbDialog, gpsDialog, pressureDialog, vibrationDialog, calorieDialog, airDialog, identityDialog, cameraDialog;
    private MonthDialog intensityDialog;
    TitanSwitchInfo titanSwitchInfo;

    TiTanFormat tiTanFormat;
    HourSystemDialog hourSystemDialog;
    DeviceUnitDialog distanceDialog;
    TemperatureUnitDialog bodyDialog, weatherDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                String sNum = (String) msg.obj;
                activityTitanBinding.serialNum.setText(sNum);

            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                TitanAgpsInfo agpsInfo = (TitanAgpsInfo) msg.obj;
                if (agpsInfo != null) {
                    activityTitanBinding.agpsInfo.setText(JSONObject.toJSONString(agpsInfo));
                }
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                TitanVersion titanVersion = (TitanVersion) msg.obj;
                if (titanVersion != null) {
                    activityTitanBinding.version.setText(titanVersion.getFirmVersion());
                    activityTitanBinding.gpsVersion.setText(titanVersion.getGpsVersion());
                }

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                titanSwitchInfo = (TitanSwitchInfo) msg.obj;
                if (titanSwitchInfo != null) {
                    activityTitanBinding.reminderSwitch.setText(titanSwitchInfo.getNotificationState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.brightScreenSwitch.setText(titanSwitchInfo.getFlickState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.disturbSwitch.setText(titanSwitchInfo.getDisturbState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.gpsSwitch.setText(titanSwitchInfo.getGpsState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.pressureSwitch.setText(titanSwitchInfo.getStressState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.vibrationMode.setText(titanSwitchInfo.getVibrationState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.calorieSwitch.setText(titanSwitchInfo.getCalorieState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.airplaneMode.setText(titanSwitchInfo.getAirplaneState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanBinding.titanOpenIdentify.setText(titanSwitchInfo.getAutoState() > 0 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));

                }

            } else if (msg.what == 0x44) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
            } else if (msg.what == 0x45) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(TitanActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x46) {
                tiTanFormat = (TiTanFormat) msg.obj;
                if (tiTanFormat != null) {
                    activityTitanBinding.bodyTemp.setText(tiTanFormat.isBodyCentigrade() ? getString(R.string.temperature_unit_centigrade) : getString(R.string.temperature_unit_fahrenheit));
                    activityTitanBinding.distanceUnit.setText(tiTanFormat.isMetric() ? getString(R.string.device_unit_metric) : getString(R.string.device_unit_imperial));
                    activityTitanBinding.temperatureUnit.setText(tiTanFormat.isWeatherCentigrade() ? getString(R.string.temperature_unit_centigrade) : getString(R.string.temperature_unit_fahrenheit));
                    activityTitanBinding.hourSystem.setText(tiTanFormat.isHour24() ? getString(R.string.twenty_four_hour_system) : getString(R.string.twelve_hour_system));
                }
            } else if (msg.what == 0x47) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
            } else if (msg.what == 0x48) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(TitanActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x49) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                int nVibrate = (int) msg.obj;
                activityTitanBinding.vibration.setText(nVibrate + "");
            }
        }
    };
    ActivityTitanBinding activityTitanBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityTitanBinding = ActivityTitanBinding.inflate(getLayoutInflater());
        setContentView(activityTitanBinding.getRoot());
        activityTitanBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityTitanBinding.tool);
        getSupportActionBar().setTitle(null);
        activityTitanBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            //查询序列号
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_serial_number, new TitanSerialNumberCallback() {
                @Override
                public void serialNum(String num) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = num;
                        mHandler.sendMessage(message);
                    }

                }

                @Override
                public void mutualFail(int errorCode) {

                }
            });
            //查询agps信息
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_agps, new TitanAgpsCallback() {
                @Override
                public void agpsInfo(TitanAgpsInfo taitanAgpsInfo) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x41;
                        message.obj = taitanAgpsInfo;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {

                }
            });
            //版本号
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_firm_version, new TitanVersionCallback() {
                @Override
                public void versionInfo(TitanVersion taitanVersion) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x42;
                        message.obj = taitanVersion;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {

                }
            });
            //查询开关信息
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_switch_info, new TitanSwitchCallback() {
                @Override
                public void switchState(TitanSwitchInfo taitanSwitchInfo) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x43;
                        message.obj = taitanSwitchInfo;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {

                }
            });
            //查询单位
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_unit_format, new TitanUnitFormatCallback() {
                @Override
                public void unitFormat(TiTanFormat taiTanFormat) {
                    if (mHandler != null) {
                        Message message = new Message();
                        message.what = 0x46;
                        message.obj = taiTanFormat;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {

                }
            });
            //查询震动
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_vibrate, new TitanVibrateCallback() {
                @Override
                public void vibration(int i) {
                    if (mHandler != null) {
                        Message message = new Message();
                        message.what = 0x49;
                        message.obj = i;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int i) {

                }
            });
        }
        activityTitanBinding.reminderSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (notifyDialog == null) {
                        notifyDialog = new SwitchDialog(TitanActivity.this);
                        notifyDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.reminderSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setNotificationState(1);
                                } else {
                                    titanSwitchInfo.setNotificationState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }

                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!notifyDialog.isShowing()) {
                        notifyDialog.show();
                    }
                }


            }
        });
        activityTitanBinding.brightScreenSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (brightDialog == null) {
                        brightDialog = new SwitchDialog(TitanActivity.this);
                        brightDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.brightScreenSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setFlickState(1);
                                } else {
                                    titanSwitchInfo.setFlickState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!brightDialog.isShowing()) {
                        brightDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.disturbSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (disturbDialog == null) {
                        disturbDialog = new SwitchDialog(TitanActivity.this);
                        disturbDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.disturbSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setDisturbState(1);
                                } else {
                                    titanSwitchInfo.setDisturbState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!disturbDialog.isShowing()) {
                        disturbDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.gpsSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (gpsDialog == null) {
                        gpsDialog = new SwitchDialog(TitanActivity.this);
                        gpsDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.gpsSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setGpsState(1);
                                } else {
                                    titanSwitchInfo.setGpsState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!gpsDialog.isShowing()) {
                        gpsDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.pressureSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (pressureDialog == null) {
                        pressureDialog = new SwitchDialog(TitanActivity.this);
                        pressureDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.pressureSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setStressState(1);
                                } else {
                                    titanSwitchInfo.setStressState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }

                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!pressureDialog.isShowing()) {
                        pressureDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.vibrationMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (vibrationDialog == null) {
                        vibrationDialog = new SwitchDialog(TitanActivity.this);
                        vibrationDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.vibrationMode.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setVibrationState(1);
                                } else {
                                    titanSwitchInfo.setVibrationState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!vibrationDialog.isShowing()) {
                        vibrationDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.calorieSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (calorieDialog == null) {
                        calorieDialog = new SwitchDialog(TitanActivity.this);
                        calorieDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.calorieSwitch.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setCalorieState(1);
                                } else {
                                    titanSwitchInfo.setCalorieState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!calorieDialog.isShowing()) {
                        calorieDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.airplaneMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (airDialog == null) {
                        airDialog = new SwitchDialog(TitanActivity.this);
                        airDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.airplaneMode.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setAirplaneState(1);
                                } else {
                                    titanSwitchInfo.setAirplaneState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!airDialog.isShowing()) {
                        airDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.titanOpenIdentify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {


                    if (identityDialog == null) {
                        identityDialog = new SwitchDialog(TitanActivity.this);
                        identityDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.titanOpenIdentify.setText(sex);
                                if (titanSwitchInfo == null) {
                                    titanSwitchInfo = new TitanSwitchInfo();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanSwitchInfo.setAutoState(1);
                                } else {
                                    titanSwitchInfo.setAutoState(0);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().openTitanNotify(titanSwitchInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x45);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!identityDialog.isShowing()) {
                        identityDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.bodyTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (bodyDialog == null) {
                        bodyDialog = new TemperatureUnitDialog(TitanActivity.this);
                        bodyDialog.setSelectListener(new TemperatureUnitDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.bodyTemp.setText(sex);
                                if (tiTanFormat == null) {
                                    tiTanFormat = new TiTanFormat();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.temperature_unit_centigrade))) {
                                    tiTanFormat.setBodyCentigrade(true);
                                } else {
                                    tiTanFormat.setBodyCentigrade(false);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanUnitFormat(tiTanFormat, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!bodyDialog.isShowing()) {
                        bodyDialog.show();
                    }
                }

            }
        });
        activityTitanBinding.distanceUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (distanceDialog == null) {
                        distanceDialog = new DeviceUnitDialog(TitanActivity.this);
                        distanceDialog.setSelectListener(new DeviceUnitDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.distanceUnit.setText(sex);
                                if (tiTanFormat == null) {
                                    tiTanFormat = new TiTanFormat();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.device_unit_metric))) {
                                    tiTanFormat.setMetric(true);
                                } else {
                                    tiTanFormat.setMetric(false);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanUnitFormat(tiTanFormat, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!distanceDialog.isShowing()) {
                        distanceDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.temperatureUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (weatherDialog == null) {
                        weatherDialog = new TemperatureUnitDialog(TitanActivity.this);
                        weatherDialog.setSelectListener(new TemperatureUnitDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.temperatureUnit.setText(sex);
                                if (tiTanFormat == null) {
                                    tiTanFormat = new TiTanFormat();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.temperature_unit_centigrade))) {
                                    tiTanFormat.setWeatherCentigrade(true);
                                } else {
                                    tiTanFormat.setWeatherCentigrade(false);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanUnitFormat(tiTanFormat, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!weatherDialog.isShowing()) {
                        weatherDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.hourSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (hourSystemDialog == null) {
                        hourSystemDialog = new HourSystemDialog(TitanActivity.this);
                        hourSystemDialog.setSelectListener(new HourSystemDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.hourSystem.setText(sex);
                                if (tiTanFormat == null) {
                                    tiTanFormat = new TiTanFormat();
                                }
                                if (sex.equalsIgnoreCase(getString(R.string.twenty_four_hour_system))) {
                                    tiTanFormat.setHour24(true);
                                } else {
                                    tiTanFormat.setHour24(false);
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanUnitFormat(tiTanFormat, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!hourSystemDialog.isShowing()) {
                        hourSystemDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.titanCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (cameraDialog == null) {
                        cameraDialog = new SwitchDialog(TitanActivity.this);
                        cameraDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanBinding.titanCamera.setText(sex);
                                int open;
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_close))) {
                                    open = 0;
                                } else {
                                    open = 1;
                                }
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().titanReadyTake(open, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }

                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!cameraDialog.isShowing()) {
                        cameraDialog.show();
                    }
                }
            }
        });
        activityTitanBinding.vibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (intensityDialog == null) {
                        intensityDialog = new MonthDialog(TitanActivity.this, getString(R.string.vibration));
                        intensityDialog.setSelectListener(new MonthDialog.SelectListener() {
                            @Override
                            public void selectData(int sex) {
                                activityTitanBinding.vibration.setText(sex + "");
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanVibrate(sex, new GeneralCallback() {
                                    @Override
                                    public void result(boolean b, int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x47);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x48);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!intensityDialog.isShowing()) {
                        intensityDialog.show();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        super.onDestroy();
    }
}
